package com.rcklos.bottomnavgationdemo.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.rcklos.bottomnavgationdemo.R;
import com.rcklos.bottomnavgationdemo.view.BlankFragment;

import java.util.ArrayList;

import static androidx.fragment.app.FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

public class Nav3Activity extends AppCompatActivity {

    private ArrayList<BlankFragment> fragments = new ArrayList<>();
    private ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav3);

        viewPager = findViewById(R.id.nav3_vp);

        fragments.add(BlankFragment.newInstance("Home"));
        fragments.add(BlankFragment.newInstance("Contacts"));
        fragments.add(BlankFragment.newInstance("User"));

        viewPager.setAdapter(myFragAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((BottomNavigationView)findViewById(R.id.nav3_nav))
                        .getMenu().getItem(position)
                        .setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        ((BottomNavigationView)findViewById(R.id.nav3_nav))
                .setOnNavigationItemSelectedListener(item -> {
                    switch (item.getItemId()){
                        case R.id.nav_home:
                            viewPager.setCurrentItem(0);
                            break;
                        case R.id.nav_contacts:
                            viewPager.setCurrentItem(1);
                            break;
                        case R.id.nav_user:
                            viewPager.setCurrentItem(2);
                            break;
                    }
                    return false;
                });

        viewPager.setCurrentItem(0);
    }

    private FragmentPagerAdapter myFragAdapter =
            new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
                @NonNull
                @Override
                public Fragment getItem(int position) {
                    return fragments.get(position);
                }

                @Override
                public int getCount() {
                    return fragments.size();
                }
            };

}
